﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Globalization;
using Windows.Storage;

namespace RaisingStudio.SmallProgram.Library
{
    [SmallProgramType]
    public static class Network
    {
        public static Primitive DownloadFile(Primitive url)
        {
            // TODO:
            /*
            if (url.IsEmpty)
            {
                return url;
            }
            string tempFileName = StorageFolder.GetTempFileName();
            Stream stream = null;
            Stream responseStream = null;
            System.Net.WebResponse response = null;
            try
            {
                response = System.Net.WebRequest.Create((string)url).GetResponse();
                stream = System.IO.File.Open(tempFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                byte[] buffer = new byte[0x4000];
                long contentLength = response.ContentLength;
                responseStream = response.GetResponseStream();
                while (contentLength > 0L)
                {
                    int count = responseStream.Read(buffer, 0, 0x4000);
                    stream.Write(buffer, 0, count);
                    contentLength -= count;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return "";
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
            return tempFileName;
            */
            return null;
        }

        internal static Primitive GetLocalFile(Primitive fileNameOrUrl)
        {
            if (fileNameOrUrl != 0)
            {
                fileNameOrUrl = ((string)fileNameOrUrl).Replace('\\', '/');
            }
            return fileNameOrUrl;
        }

        public static Primitive GetWebPageContents(Primitive url)
        {
            if (url.IsEmpty)
            {
                return url;
            }
            return GetWebPageContents((string)url);
        }

        internal static string GetWebPageContents(string url)
        {
            StreamReader streamReader = null;
            System.Net.WebResponse response = null;
            string result = "";
            try
            {
                // TODO:
                //response = System.Net.WebRequest.Create(url).GetResponse();
                streamReader = new StreamReader(response.GetResponseStream());
                result = streamReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                if (streamReader != null)
                {
                    streamReader.Dispose();
                }
                if (response != null)
                {
                    response.Dispose();
                }
            }
            return result;
        }
    }
}

